package com.bykj.property.api.controller;

import com.bykj.property.api.service.WxAppRepairService;
import com.bykj.property.controller.vo.response.RespResult;
import com.bykj.property.controller.vo.view.RepairOrderVo;
import com.bykj.property.model.RepairOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Slf4j
@Api(tags = {"api微信报修"})
@RestController
@RequestMapping("/api")
public class WxAppRepairController {
    @Value("${realm}")
    private String realm;
    @Value("${imagesMap}")
    private String imagesMap;
    @Value("${imagehttp}")
    private String imagehttp;
    @Value("${server.port}")
    private String port;
    @Autowired
    private WxAppRepairService wxAppRepairService;
    @ApiOperation(value = "提交报修申请")
    @RequestMapping(value = "/user/repair", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public RespResult submitRepairOrder(@NotEmpty String openid,
                                      @NotEmpty String repair_content,
                                       String img,
                                       String repair_name,
                                       String phone) {
        int i = wxAppRepairService.submitRepairOrder(openid,repair_content,repair_name,phone,img);
        return RespResult.ok(i);
    }
    @ApiOperation(value = "查看报修列表")
    @RequestMapping(value = "/user/repair/getOrders", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public RespResult repairGetOrders(@NotEmpty String openid) {
        List<RepairOrder> repairOrders = wxAppRepairService.repairGetOrders(openid);
        return RespResult.ok(repairOrders);
    }
    @ApiOperation(value = "查看报修详情")
    @RequestMapping(value = "/user/repair/detail", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public RespResult getRepairOrderDetail(@NotEmpty String repairOrderId) {
        RepairOrderVo repairOrderVo = wxAppRepairService.getRepairOrderDetail(repairOrderId);
        return RespResult.ok(repairOrderVo);
    }
    @ApiOperation(value = "保修图片上传")
    @RequestMapping(value = "user/repair/img/upload", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String repairIrderUpload(@RequestParam("file")MultipartFile[] files) {
        String path = "";
        log.info("进入上传...");
        String uploadPath="C:/images/";//存放到本地路径（示例）
        if(files!=null && files.length>=1) {
            BufferedOutputStream bw = null;
            try {
                String fileName = files[0].getOriginalFilename();
                //判断是否有文件
                if(StringUtils.isNoneBlank(fileName)) {
                    String newFileName = UUID.randomUUID().toString() + getFileType(fileName);
                    //输出到本地路径
                    File outFile = new File(uploadPath + newFileName);
                    log.info("path=="+uploadPath + UUID.randomUUID().toString()+ getFileType(fileName));
                    path =imagehttp+"://"+ realm + ":"+ port+imagesMap + "/" + newFileName;
                    FileUtils.copyInputStreamToFile(files[0].getInputStream(), outFile);
                    // newFileName保存到数据库

                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if(bw!=null) {
                        bw.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return path;
    }

    private String getFileType(String filename) {
        if(filename.endsWith(".jpg") || filename.endsWith(".jepg")){
            return ".jpg";
        }else if(filename.endsWith(".png") || filename.endsWith(".PNG")){
            return ".png";
        } else{
            return "application/octet-stream";
        }
    }

}

